package com.ecy.quadratic.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ComicRankAdapter;
import com.ecy.quadratic.beans.Comic;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.component.DaggerComicListComponent;
import com.ecy.quadratic.reader.base.BaseActivity;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.contract.ComicListContract;
import com.ecy.quadratic.ui.presenter.ComicListPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/5.
 */
public class
ComicRankActivity extends BaseActivity implements ComicListContract.View {

    @Bind(R.id.comit_swipe_refreshview)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.comit_listview)
    ListView mComicListView;
    @Inject
    ComicListPresenter mPresenter;
    private ComicRankAdapter mAdapter;
    private List<Comic> mComicList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            mPresenter.refresh("1", "city");
        }
    };
    private SwipeRefreshView.OnLoadListener mOnLoadListener = new SwipeRefreshView.OnLoadListener() {

        @Override
        public void onLoad() {
            mPresenter.loadMore(mCurrentPage + 1 + "", "city");
        }
    };

    public static final void startActivity(Context context) {
        Intent intent = new Intent(context, ComicRankActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_comic_rank;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerComicListComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initToolBar() {
        mCommonToolbar.setNavigationIcon(R.drawable.ab_back);
        mCommonToolbar.setTitle(R.string.book_detail);
    }

    @Override
    public void initData() {
        mComicList = new ArrayList<Comic>();
        mAdapter = new ComicRankAdapter(this, mComicList);
        mComicListView.setAdapter(mAdapter);
    }

    @Override
    public void configViews() {
        mSwipeRefreshView = (SwipeRefreshView) findViewById(R.id.comit_swipe_refreshview);
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.setOnLoadListener(mOnLoadListener);
        mSwipeRefreshView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                mSwipeRefreshView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
        mComicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < mAdapter.getCount()) {
                    ComicDetailActivity.startActivity(ComicRankActivity.this, mAdapter.getItem(position));
                }
            }
        });
        mPresenter.attachView(this);
    }

    @OnClick(R.id.title_back_iv)
    public void back() {
        finish();
    }

    @Override
    public void showRefreshList(PageInfo<Comic> pageInfo) {

        if (pageInfo != null && mTotalPage != pageInfo.pages) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mComicList.clear();
            mComicList.addAll(pageInfo.list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showLoadMoreList(PageInfo<Comic> pageInfo) {
        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;

            mComicList.addAll(pageInfo.list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false, true);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

}
